package com.wechat.order.handler.ExceptionHandler;

import com.wechat.order.config.ConstantConfig.ProjectUrl;
import com.wechat.order.exception.FakeWeChatLoginException;
import com.wechat.order.exception.ResponseBankException;
import com.wechat.order.exception.SellException;
import com.wechat.order.exception.SellerAuthorizeException;
import com.wechat.order.util.ResultVOUtil;
import com.wechat.order.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * 异常控制器
 * Created by zzy on 2017/12/21.
 */
@ControllerAdvice
@Slf4j
public class SellerExceptionHandler {

    @Autowired
    private ProjectUrl projectUrl;

    //拦截登陆异常处理
    @ExceptionHandler(value = SellerAuthorizeException.class)
    public ModelAndView handlerSellerAuthorizeException(SellerAuthorizeException e){

        return new ModelAndView("redirect:".
                concat(projectUrl.getWcOpenAuthorize()).
                concat("/sell/weChat/qrAuthorize").
                concat("?returnUrl=").
                concat("/sell/seller/login"));
    }

    @ExceptionHandler(value = FakeWeChatLoginException.class)
    public ModelAndView handlerFakeWeChatLoginException(FakeWeChatLoginException e){
        return new ModelAndView("redirect:".concat(projectUrl.getLoginProxy()));

    }

    @ExceptionHandler(value = SellException.class)
    public ResultVO handlerSellException(SellException e){
        return ResultVOUtil.error(e.getCode(),e.getMessage());
    }


    @ExceptionHandler(value = ResponseBankException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlerResponseBankException(){

    }
}
